package phone.book;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentProviderOperation;
import android.content.Context;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Add_contact extends Activity{
	 
	@Override
	public void onCreate(Bundle savedInstanceState) {
					super.onCreate(savedInstanceState);
					setContentView(R.layout.addcontact);
					final EditText fname = (EditText) findViewById(R.id.editText1);
					final EditText lname = (EditText) findViewById(R.id.editText2);
					final EditText phone = (EditText) findViewById(R.id.editText3);
					final Button button1 = (Button) findViewById(R.id.button1);
					final Button button2 = (Button) findViewById(R.id.button2);	
					final Context c = this;
					button1.setOnClickListener(new View.OnClickListener() {             
						public void onClick(View v) {                 // Perform action on click             
							
							final String DisplayName =fname.getText().toString()+" "+lname.getText().toString();
							final String MobileNumber = phone.getText().toString();
							Log.d("EditText", fname.getText().toString());
							System.out.println(MobileNumber+"   "+DisplayName);
							addContact(DisplayName,MobileNumber,c);
							
						}

						     
					});
					button2.setOnClickListener(new View.OnClickListener() {             
						public void onClick(View v) {                 // Perform action on click             
								finish();
							}         
					});
					
	}
	public void addContact(String DisplayName,
			String MobileNumber, Context c) {
		ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
		ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null).withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).build());                      
		//------------------------------------------------------ Names                     
		if(DisplayName != null)                     
		{                         
			ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, DisplayName).build());                     
		}                                            
		//------------------------------------------------------ Mobile Number                     
		if(MobileNumber != null)                     
		{                         
			ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, MobileNumber).withValue(ContactsContract.CommonDataKinds.Phone.TYPE,ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE).build());                     
		}  
		
		// Asking the Contact provider to create a new contact                                       
		try                      
		{   								
			c.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);                     
		}                      
		catch (Exception e)                      
		{                                        
			e.printStackTrace();                         
			Toast.makeText(Add_contact.this, "Exception: " + e.getMessage(), Toast.LENGTH_SHORT).show();                     
		} 
	
		finish();
	}    
}
